package com.demo.controller.api;

import com.demo.common.Result;
import com.demo.common.annotation.CustomAnnotation;
import com.demo.common.message.BaseResponse;
import com.demo.controller.BaseController;
import com.demo.entities.User;
import com.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

import static com.demo.common.util.PropertiesUtil.value;

/**
 * 用户控制类
 * Created by Administrator on 2016/8/22.
 */
@RestController
@RequestMapping("/api")
public class UserApiCtrl extends BaseController {

    @Autowired
    private IUserService userService;


    /**
     * 获取全部用户信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
    @CustomAnnotation(permission = "load")
    public BaseResponse loadUserById(@PathVariable Integer id) throws Exception {
        User user = userService.loadUserById(id);
        return toSuccess(value("load_successful"), user);
    }

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public BaseResponse insertUser(User user) throws Exception {
        boolean status = userService.insertUser(user);
        return toSuccess(value("load_successful"), status);
    }

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public BaseResponse loadUsersByPage(@RequestAttribute String keyword) throws Exception {
        Result result = userService.loadUsersByPage(keyword, super.page);
        return toSuccess(value("msg.load_successful"), result);
    }

    @RequestMapping(value = "user/client/deleteUser/{id}", method = RequestMethod.DELETE)
    public BaseResponse deleteUser(@PathVariable String id) throws Exception {
        boolean status = userService.deleteUser(id);
        return toSuccess(status);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseResponse login(@RequestParam("id") Integer id, @RequestParam("password") String password) throws Exception {
        User user1 = userService.loadUserById(id);
        if (user1 == null) {
            return toError("帐号不存在");
        }
        User user2 = userService.login(id, password);
        if (user2 == null) {
            return toError("密码不正确");
        }
        session.setAttribute("currentUser", user2);
        return toSuccess("登录成功");
    }

    @RequestMapping(value = "user/client/updateUser", method = RequestMethod.POST)
    public BaseResponse updateUser(String id, String name, String age, String cs) throws Exception {
        boolean status = userService.updateUser(id, name, age, cs);
        return toSuccess(status);
    }

    @RequestMapping(value = "user/client/updateUserOne", method = RequestMethod.GET)
    public BaseResponse updateUserOne(User user) throws Exception {
        boolean status = userService.updateUser(user);
        return new BaseResponse("100", status);
    }


    @RequestMapping(value = "user/client/selectOneC/{id}", method = RequestMethod.GET)
    public BaseResponse selectOneC(@PathVariable String id) throws Exception {
        String val = userService.selectUserName(id);
        return toSuccess(val);
    }

    @RequestMapping(value = "user/client/selectOne/{id}", method = RequestMethod.GET)
    public BaseResponse selectOne(@PathVariable String id) throws Exception {
        User user = userService.selectOne(id);
        return toSuccess(user);
    }


    /**
     * 页面跳转
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("browser/toLogin")
    @ResponseBody
    public ModelAndView toLoginPage(HttpSession session) throws Exception {
        ModelAndView mv = new ModelAndView();
        session.removeAttribute("currentUser");
        mv.setViewName("redirect:/view/login.jsp");
        return mv;
    }
}
